package com.midea.ai.air.view;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;

/**
 * Created by hww on 2016/3/22.
 * <p/>
 * 标题栏通用
 */
public class MyTitleBar {

    private Context context;
    private View view;

    private TextView left_TV;
    private TextView center_TV;
    private TextView right_TV;
    private ImageView left_IV;
    private ImageView right_IV;
    private RelativeLayout left_back;
    private RelativeLayout bg_color;

    public MyTitleBar(Context context) {
        this.context = context;
        setActionBar_for_Activity();
    }

    public MyTitleBar(Fragment fragment, View view) {
        this.view = view;
        setActionBar_for_Fragment(fragment, view);
    }


    private void setActionBar_for_Activity() {
        left_IV = (ImageView) ((Activity) context).findViewById(R.id.actionbar_iv_left);
        right_IV = (ImageView) ((Activity) context).findViewById(R.id.actionbar_iv_right);
        left_TV = (TextView) ((Activity) context).findViewById(R.id.actionbar_tv_left);
        right_TV = (TextView) ((Activity) context).findViewById(R.id.actionbar_tv_right);
        center_TV = (TextView) ((Activity) context).findViewById(R.id.actionbar_tv_center);
        left_back = (RelativeLayout) ((Activity) context).findViewById(R.id.actionbar_left_relativeLayout);
        bg_color = (RelativeLayout) ((Activity) context).findViewById(R.id.action_bar_activity_relativelayout);

        //控件点击事件
        left_IV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
        left_TV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) context).finish();
            }
        });
        right_IV.setOnClickListener((View.OnClickListener) context);
        right_TV.setOnClickListener((View.OnClickListener) context);
        center_TV.setOnClickListener((View.OnClickListener) context);
        left_back.setOnClickListener((View.OnClickListener) context);
    }


    private void setActionBar_for_Fragment(Fragment fragment, View view) {
        right_IV = (ImageView) view.findViewById(R.id.actionbar_iv_right);
        left_IV = (ImageView) view.findViewById(R.id.actionbar_iv_left);
        left_TV = (TextView) view.findViewById(R.id.actionbar_tv_left);
        right_TV = (TextView) view.findViewById(R.id.actionbar_tv_right);
        center_TV = (TextView) view.findViewById(R.id.actionbar_tv_center);
        left_back = (RelativeLayout) view.findViewById(R.id.actionbar_left_relativeLayout);
        bg_color = (RelativeLayout) view.findViewById(R.id.action_bar_activity_relativelayout);

        //控件点击事件
        left_IV.setOnClickListener((View.OnClickListener) fragment);
        left_TV.setOnClickListener((View.OnClickListener) fragment);
        right_IV.setOnClickListener((View.OnClickListener) fragment);
        right_TV.setOnClickListener((View.OnClickListener) fragment);
        center_TV.setOnClickListener((View.OnClickListener) fragment);
        left_back.setOnClickListener((View.OnClickListener) fragment);
    }

    /**
     * 设置ActionBar的左边控件
     *
     * @param isShowleft_iv 左边图标是否显示
     * @param R_drawable    左图标的图标资源
     * @param R_String      左文字的资源
     */
    public void setActioBar_left(boolean isShowleft_iv, int R_drawable, int R_String) {
        if (isShowleft_iv) {
            left_IV.setVisibility(View.VISIBLE);
            left_IV.setImageResource(R_drawable);
        } else {
            left_IV.setVisibility(View.GONE);
        }
        left_TV.setText(R_String);
    }

    /**
     * 设置ActionBar的中间控件
     *
     * @param R_resId 中间TextView的文字资源
     */
    public void setActionBar_center(int R_resId) {
        center_TV.setText(R_resId);
    }

    public void setActionBar_center(String R_string) {
        center_TV.setText(R_string);
    }


    /**
     * 设置ActionBar的右边控件
     *
     * @param R_string 右边TextView的文字资源
     */
    public void setActionBar_right(boolean isShow_tv, int R_string) {
        if (isShow_tv) {
            right_IV.setVisibility(View.GONE);
            right_TV.setVisibility(View.VISIBLE);
            right_TV.setText(R_string);
        } else {
            right_TV.setVisibility(View.GONE);
        }
    }

    /**
     * 设置ActionBar右边的图片设置
     *
     * @param isShow_iv
     * @param R_resId
     */
    public void setActionBar_right_iv(boolean isShow_iv, int R_resId) {
        if (isShow_iv) {
            right_TV.setVisibility(View.GONE);
            right_IV.setVisibility(View.VISIBLE);
            right_IV.setImageResource(R_resId);
        } else {
            right_IV.setVisibility(View.GONE);
        }
    }

    public void setTitleBarBG(int resId) {
        bg_color.setBackgroundResource(resId);
    }

}
